package com.lmabbe.core.service.mq;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.lmabbe.common.datasources.handle.InitApplicationContext;
import com.lmabbe.common.datasources.handle.InitApplicationFactory;
import com.lmabbe.common.datasources.handle.InitApplicationHandle;
import com.lmabbe.common.global.context.TenantBuildContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 初始化租户处理器
 *
 * @author lmabbe
 */
@Slf4j
@Component
public class InitTenantHandle {


    @KafkaListener(topics = {"system"}, groupId = "#{T(java.util.UUID).randomUUID()})")
    public void initTenant(ConsumerRecord<String, Object> record) {
        log.error("接收到消息===>"+ JSONObject.toJSONString(record));
        String key = record.key();
        System.out.println(key);
        if("tenant".equals(key)){
            TenantBuildContext tenantBuildContext = (TenantBuildContext) record.value();
            InitApplicationHandle mqChain = InitApplicationFactory.createMqChain(SpringUtil.getApplicationContext(), tenantBuildContext);
            mqChain.exec(new InitApplicationContext());
        }
    }
}
